package com.guldan.jaina.manager;

import com.guldan.mapper.db.entity.ProjectRegisterUploadExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectregisterupload.ProjectRegisterUploadDTO;
import com.guldan.jaina.utils.ProjectRegisterUploadDomainConvertUtils;
import com.guldan.mapper.db.entity.ProjectRegisterUpload;
import com.guldan.mapper.db.mapper.ProjectRegisterUploadMapperExt;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:35
 */
@Service
public class ProjectRegisterUploadManager extends BaseManager {
    @Autowired
    private ProjectRegisterUploadMapperExt projectRegisterUploadMapperExt;

    public ProjectRegisterUploadDTO save(ProjectRegisterUploadDTO dto) {
        ProjectRegisterUpload entity;
        if (dto.getUploadId() != null && dto.getUploadId() > 0) {
            entity = projectRegisterUploadMapperExt.selectByPrimaryKey(dto.getUploadId());
            entity.setBatchId(dto.getBatchId());
            entity.setFailNum(dto.getFailNum());
            entity.setProjectId(dto.getProjectId());
            entity.setSuccessNum(dto.getSuccessNum());
            entity.setYear(dto.getYear());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectRegisterUploadMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectRegisterUpload();
            entity.setBatchId(dto.getBatchId());
            entity.setCreateTime(DateUtil.getCurrentTime());
            entity.setFailNum(dto.getFailNum());
            entity.setProjectId(dto.getProjectId());
            entity.setSuccessNum(dto.getSuccessNum());
            entity.setYear(dto.getYear());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectRegisterUploadMapperExt.insertSelective(entity);
        }
        dto = ProjectRegisterUploadDomainConvertUtils.convert(entity);
        return dto;
    }

    public ProjectRegisterUploadDTO get(Integer uploadId) {
        return ProjectRegisterUploadDomainConvertUtils.convert(projectRegisterUploadMapperExt.selectByPrimaryKey(uploadId));
    }

    public List<ProjectRegisterUploadDTO> listUploadRecord(Integer batchId) {
        ProjectRegisterUploadExample example = new ProjectRegisterUploadExample();
        ProjectRegisterUploadExample.Criteria criteria = example.createCriteria();
        criteria.andBatchIdEqualTo(batchId);
        criteria.andIsDeleteEqualTo(false);
        example.setOrderByClause("upload_id desc");
        List<ProjectRegisterUpload> entityList = projectRegisterUploadMapperExt.selectByExample(example);
        List<ProjectRegisterUploadDTO> dtoList = new ArrayList<>();
        if (entityList != null && entityList.size() > 0) {
            for (ProjectRegisterUpload entity : entityList) {
                dtoList.add(ProjectRegisterUploadDomainConvertUtils.convert(entity));
            }
        }
        return dtoList;
    }
    
}
